package terra.network.packet.client;

import java.io.UnsupportedEncodingException;

import org.jboss.netty.buffer.ChannelBuffer;

import terra.network.packet.Packet;

// byte int str int str
public class RequestLogIn extends Packet {

	public String name, pass;
	
	public RequestLogIn() {
	}

	public RequestLogIn(String text, String text2) {
		name = text;
		pass = text2;
	}

	@Override
	public ChannelBuffer toBuffer() {
		byte[] n = null,s = null,p = null;
		try {
			n = name.getBytes("UTF-8");
			s = "/n".getBytes("UTF-8");
			p = pass.getBytes("UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		ChannelBuffer buf = getNewBuffer(1 + 4 + n.length + p.length + s.length);
		buf.writeByte(getId());
		buf.writeInt(n.length + p.length + s.length);
		buf.writeBytes(n);
		buf.writeBytes(s);
		buf.writeBytes(p);
		return buf;
	}

	@Override
	public Packet fromBuffer(ChannelBuffer buf) {
		int length = buf.readInt();
		byte[] nameBytes = new byte[length];
		buf.readBytes(nameBytes);
		String all = null;
		try {
			all = new String(nameBytes,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		//System.out.println("all " + all);
		String[] s = all.split("/n");
		name = s[0];
		pass = s[1];
		return this;
	}

	@Override
	public int getLength() {
		return 0;
	}

	@Override
	public byte getId() {
		return 1;
	}

	@Override
	public byte getPriority() {
		return PRIOR_NORMAL;
	}
	
	@Override
	public boolean isDynamicLength() {
		return true;
	}

}
